

package com.zdsx.service.impl;


import com.github.xiaoymin.knife4j.core.util.StrUtil;
import com.zdsx.common.utils.ConvertUtils;

import com.zdsx.dao.ProductInfoDao;
import com.zdsx.dto.GasmeterTypeDTO;
import com.zdsx.dto.ProductInfoDTO;
import com.zdsx.entity.ProductInfo;
import com.zdsx.entity.UserEntity;
import com.zdsx.service.ProductService;
import com.zdsx.service.SysParamsService;
import com.zdsx.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 异常日志
 *
 * @author Lr
 * @since 1.0.0
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Resource
    private ProductInfoDao productInfoDao;

    @Resource
    private UserService userService;

    @Resource
    private SysParamsService sysParamsService;

    /**
     * 获取产品列表
     * @return null
     */
    @Override
    public List<ProductInfoDTO> productList(Long userId) {

        // 产品列表开关
        String productListSwitch = "product_list_switch";
        String value = sysParamsService.getValue(productListSwitch);
        if ("1".equals(value)) {
            List<ProductInfo> productInfos = productInfoDao.getProductByMobile(null);
            return ConvertUtils.sourceToTarget(productInfos, ProductInfoDTO.class);
        }

        // 根据userId找到手机号码
        UserEntity userEntity = userService.getUserByUserId(userId);
        String mobile = userEntity.getMobile();

        if (StrUtil.isNotBlank(mobile)) {
            // 产品列表开关
            // 先查询用户角色，再查询产品id列表
            List<ProductInfo> productInfos = productInfoDao.getProductByMobile(mobile);
            return ConvertUtils.sourceToTarget(productInfos, ProductInfoDTO.class);
        }
        return new ArrayList<>();
    }

    @Override
    public List<GasmeterTypeDTO> gasmeterList() {
        return productInfoDao.getGasmeterType();
    }

}